//
//  ShopSeraschController.m
//  card
//
//  Created by 韦显光 on 2017/8/1.
//  Copyright © 2017年 xg韦. All rights reserved.
//

#import "ShopSeraschController.h"
#import "CollCell.h"
#import "CollHerad.h"
#import "CarCollCell.h"
#import "EqualSpaceFlowLayoutEvolve.h"
#import "NilCell.h"
#import "SearchListController.h"
@interface ShopSeraschController ()
{
    NSArray*search_hot;
    NSArray*search_history;

}
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flow2;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flow;
- (IBAction)back:(id)sender;
@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
@property (weak, nonatomic) IBOutlet UICollectionView *clloview1;
@property (weak, nonatomic) IBOutlet UIView *navback;

@end

@implementation ShopSeraschController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITextField *searchField = [_searchbar valueForKey:@"_searchField"];
    [searchField setFont:[UIFont systemFontOfSize:14]];
    EqualSpaceFlowLayoutEvolve * flowLayout = [[EqualSpaceFlowLayoutEvolve alloc]initWthType:AlignWithLeft];
    _flow=flowLayout;
    [_clloview1 setCollectionViewLayout:_flow];

    [FuncPublic getData:self parameters:nil RequestUrl:Csearch_history success:^(NSDictionary *acountInfo) {
        search_hot=acountInfo[@"search_hot"];
//        search_history=acountInfo[@"search_history"];
        search_history=@[@"大受打击我",@"撒大声地",@"ewer"];
        [_clloview1 reloadData];
        NSLog(@"%@",acountInfo);

    }];

    
    
}


-(void)loadsearchHistory
{
//    [FuncPublic getData:self parameters:@{@"limit":@"20",@"page":@"1"} RequestUrl:searchHistory success:^(NSDictionary *acountInfo) {
//        colldata2=acountInfo[@"data"][@"histories"];
//        [_clloview2 reloadData];
//    }];

}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_searchbar resignFirstResponder];
}

#pragma mark -- UICollectionViewDataSource 数据源方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    NSString*lb1str=indexPath.section==0?search_hot[indexPath.row]:search_history[indexPath.row];
        return CGSizeMake([MyUtils GetWidthFromString:lb1str Font:[UIFont systemFontOfSize:14] Height:30]+15 ,35);
  
}

//返回section 的数量
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return section==0?search_hot.count:search_history.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        static NSString *ID = @"CarCollCell";
       NSString*lb1str=indexPath.section==0?search_hot[indexPath.row]:search_history[indexPath.row];
        CarCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        cell.lb.text=lb1str;
        cell.lb.layer.cornerRadius=3;
        cell.lb.layer.masksToBounds=YES;
        [[cell contentView] setFrame:[cell bounds]];
        [[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        
        return cell;
        
}

#pragma mark --UICollectionViewDelegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  
        NSString*lb1str=indexPath.section==0?search_hot[indexPath.row]:search_history[indexPath.row];
         [self loaadsruechname:lb1str];
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(DEVW, 45);
}

//设置header和footer的view
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    CollHerad*Herad_view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollHerad" forIndexPath:indexPath];
    Herad_view.completion = ^(UIButton *changgeBtn) {
        
    };
    if (indexPath.section==0) {
        Herad_view.lb1.text=@"热搜";
        Herad_view.btno.hidden=YES;
    }else
    {
        Herad_view.lb1.text=@"历史记录";
        Herad_view.btno.hidden=NO;
    }
    return Herad_view;
    
}


-(void)loaadsruechname:(NSString*)name
{
  
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"NewHome" bundle:nil];
    SearchListController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"SearchList"];
    secondViewController.PassKeyWork=name;
    [self.navigationController pushViewController:secondViewController animated:YES];

}







//点击键盘上的search按钮时调用

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar

{
    if ([searchBar.text empty]) {
        return;
    }
    [searchBar resignFirstResponder];
    [self loaadsruechname:searchBar.text];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)back:(id)sender {
 [self.navigationController popViewControllerAnimated:YES];
}
@end
